package com.sxx.beautifulcity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.VillageData;
import com.sxx.beautifulcity.entity.greendao.VillageDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class VillageAdapter extends RecyclerView.Adapter<VillageAdapter.VillageAdapterViewHolder> {
    private List<VillageDaoEntity> list;
    private OnMyItemClickListener listener;

    public void setOnMyItemClick(OnMyItemClickListener l){
        listener = l;
    }

    public VillageAdapter(Context context) {

    }
    /**添加数据
     * @param datas*/
    public void addData(List<VillageDaoEntity> datas) {
        list = datas;
        notifyDataSetChanged();
    }
    /***对组件进行初始化*/
    class VillageAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private SimpleDraweeView village_draweeView;//乡村图片
        private TextView villageNameTv;//乡村名
        private RatingBar villageRB;//星评价
        private LinearLayout village_linearlayout;
        private int position;

        public VillageAdapterViewHolder(View v) {
            super(v);
            village_draweeView = (SimpleDraweeView) v.findViewById(R.id.village_draweeView);
            villageNameTv = (TextView) v.findViewById(R.id.villageNameTv);
            villageRB = (RatingBar) v.findViewById(R.id.villageRB);
            village_linearlayout = (LinearLayout) v.findViewById(R.id.village_linearlayout);
            village_linearlayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                VillageDaoEntity data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }

    /**加载行布局*/
    @Override
    public VillageAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_village_item, null);

        return new VillageAdapterViewHolder(v);
    }
    /**绑定数据*/
    @Override
    public void onBindViewHolder(VillageAdapterViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            VillageDaoEntity data = list.get(position);
            holder.villageNameTv.setText(data.getName());
            holder.villageRB.setNumStars(data.getStar());
            if (data.getImage() != null && data.getImage().length() > 0) {
                String path = data.getImage();
                frescoImage(path, holder.village_draweeView);
            }
        }
        holder.position = position;
    }
    /**三级缓存*/
    public void frescoImage(String path, SimpleDraweeView draweeView) {
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {

        return list != null && list.size() > 0 ? list.size() : 0;
    }
}
